package org.rsbot.script.randoms;

import java.awt.Point;
import java.io.PrintStream;
import java.util.logging.Logger;
import org.rsbot.script.Random;
import org.rsbot.script.ScriptManifest;
import org.rsbot.script.methods.Calculations;
import org.rsbot.script.methods.Camera;
import org.rsbot.script.methods.Interfaces;
import org.rsbot.script.methods.Menu;
import org.rsbot.script.methods.Mouse;
import org.rsbot.script.methods.NPCs;
import org.rsbot.script.methods.Objects;
import org.rsbot.script.methods.Walking;
import org.rsbot.script.wrappers.RSCharacter;
import org.rsbot.script.wrappers.RSComponent;
import org.rsbot.script.wrappers.RSInterface;
import org.rsbot.script.wrappers.RSNPC;
import org.rsbot.script.wrappers.RSObject;
import org.rsbot.script.wrappers.RSPath;
import org.rsbot.script.wrappers.RSPlayer;

@ScriptManifest(authors={"Arbiter", "PwnZ", "Megaalgos", "Taha", "Fred", "Poxer", "Iscream"}, name="Exam", version=2.1D)
public class Exam extends Random
{
  public static final int nextObjectInterface = 103;
  public static final int relatedCardsInterface = 559;
  public static final int[] Ranged = { 11539, 11540, 11541, 11614, 11615, 11633 };

  public static final int[] Cooking = { 11526, 11529, 11545, 11549, 11550, 11555, 11560, 11563, 11564, 11607, 11608, 11616, 11620, 11621, 11622, 11623, 11628, 11629, 11634, 11639, 11641, 11649, 11624 };

  public static final int[] Fishing = { 11527, 11574, 11578, 11580, 11599, 11600, 11601, 11602, 11603, 11604, 11605, 11606, 11625 };

  public static final int[] Combat = { 11528, 11531, 11536, 11537, 11579, 11591, 11592, 11593, 11597, 11627, 11631, 11635, 11636, 11638, 11642, 11648, 11617 };

  public static final int[] Farming = { 11530, 11532, 11547, 11548, 11554, 11556, 11571, 11581, 11586, 11610, 11645 };

  public static final int[] Magic = { 11533, 11534, 11538, 11562, 11567, 11582 };

  public static final int[] Firemaking = { 11535, 11551, 11552, 11559, 11646 };

  public static final int[] Hats = { 11540, 11557, 11558, 11560, 11570, 11619, 11626, 11630, 11632, 11637, 11654 };

  public static final int[] Pirate = { 11570, 11626, 11558 };

  public static final int[] Jewellery = { 11572, 11576, 11652 };

  public static final int[] Jewellery2 = { 11572, 11576, 11652 };

  public static final int[] Drinks = { 11542, 11543, 11544, 11644, 11647 };

  public static final int[] Woodcutting = { 11573, 11595 };

  public static final int[] Boots = { 11561, 11618, 11650, 11651 };

  public static final int[] Crafting = { 11546, 11553, 11565, 11566, 11568, 11569, 11572, 11575, 11576, 11577, 11581, 11583, 11584, 11585, 11643, 11652, 11653 };

  public static final int[] Mining = { 11587, 11588, 11594, 11596, 11598, 11609, 11610 };

  public static final int[] Smithing = { 11611, 11612, 11613 };
  public static final int[][] items = { Ranged, Cooking, Fishing, Combat, Farming, Magic, Firemaking, Hats, Drinks, Woodcutting, Boots, Crafting, Mining, Smithing };

  public SimilarObjectQuestion[] simObjects = { new SimilarObjectQuestion("I never leave the house without some sort of jewellery.", Jewellery), new SimilarObjectQuestion("There is no better feeling than", Jewellery2), new SimilarObjectQuestion("I'm feeling dehydrated", Drinks), new SimilarObjectQuestion("All this work is making me thirsty", Drinks), new SimilarObjectQuestion("quenched my thirst", Drinks), new SimilarObjectQuestion("light my fire", Firemaking), new SimilarObjectQuestion("fishy", Fishing), new SimilarObjectQuestion("fishing for answers", Fishing), new SimilarObjectQuestion("fish out of water", Drinks), new SimilarObjectQuestion("strange headgear", Hats), new SimilarObjectQuestion("tip my hat", Hats), new SimilarObjectQuestion("thinking cap", Hats), new SimilarObjectQuestion("wizardry here", Magic), new SimilarObjectQuestion("rather mystical", Magic), new SimilarObjectQuestion("abracada", Magic), new SimilarObjectQuestion("hide one's face", Hats), new SimilarObjectQuestion("shall unmask", Hats), new SimilarObjectQuestion("hand-to-hand", Combat), new SimilarObjectQuestion("melee weapon", Combat), new SimilarObjectQuestion("prefers melee", Combat), new SimilarObjectQuestion("me hearties", Pirate), new SimilarObjectQuestion("puzzle for landlubbers", Pirate), new SimilarObjectQuestion("mighty pirate", Pirate), new SimilarObjectQuestion("mighty archer", Ranged), new SimilarObjectQuestion("as an arrow", Ranged), new SimilarObjectQuestion("Ranged attack", Ranged), new SimilarObjectQuestion("shiny things", Crafting), new SimilarObjectQuestion("igniting", Firemaking), new SimilarObjectQuestion("sparks from my synapses.", Firemaking), new SimilarObjectQuestion("fire.", Firemaking), new SimilarObjectQuestion("disguised", Hats), new SimilarObjectQuestion("range", Ranged), new SimilarObjectQuestion("arrow", Ranged), new SimilarObjectQuestion("drink", Drinks), new SimilarObjectQuestion("logs", Firemaking), new SimilarObjectQuestion("light", Firemaking), new SimilarObjectQuestion("headgear", Hats), new SimilarObjectQuestion("hat", Hats), new SimilarObjectQuestion("cap", Hats), new SimilarObjectQuestion("mine", Mining), new SimilarObjectQuestion("mining", Mining), new SimilarObjectQuestion("ore", Mining), new SimilarObjectQuestion("fish", Fishing), new SimilarObjectQuestion("fishing", Fishing), new SimilarObjectQuestion("thinking cap", Hats), new SimilarObjectQuestion("cooking", Cooking), new SimilarObjectQuestion("cook", Cooking), new SimilarObjectQuestion("bake", Cooking), new SimilarObjectQuestion("farm", Farming), new SimilarObjectQuestion("farming", Farming), new SimilarObjectQuestion("cast", Magic), new SimilarObjectQuestion("magic", Magic), new SimilarObjectQuestion("craft", Crafting), new SimilarObjectQuestion("boot", Boots), new SimilarObjectQuestion("chop", Woodcutting), new SimilarObjectQuestion("cut", Woodcutting), new SimilarObjectQuestion("tree", Woodcutting) };

  public RSObject door = null;

  public boolean activateCondition()
  {
    this.door = null;
    return this.npcs.getNearest(new String[] { "Mr. Mordaut" }) != null;
  }

  public boolean clickCharacter(RSCharacter paramRSCharacter, String paramString)
  {
    try
    {
      Point localPoint = paramRSCharacter.getScreenLocation();

      if ((!paramRSCharacter.isValid()) || (!this.calc.pointOnScreen(localPoint))) {
        System.out.println("Not on screen " + paramString);
        return false;
      }

      this.mouse.move(localPoint);

      localPoint = paramRSCharacter.getScreenLocation();

      String[] arrayOfString = this.menu.getItems();

      if (arrayOfString[0].toLowerCase().contains(paramString.toLowerCase())) {
        this.mouse.click(localPoint, true);
        return true;
      }
      this.mouse.click(localPoint, false);
      return this.menu.doAction(paramString);
    }
    catch (NullPointerException localNullPointerException)
    {
    }
    return true;
  }

  public boolean clickObject(RSObject paramRSObject, String paramString)
  {
    try
    {
      int i = (int)this.calc.tileToScreen(paramRSObject.getLocation()).getX();
      int j = (int)this.calc.tileToScreen(paramRSObject.getLocation()).getY();

      Point localPoint = new Point(i, j);

      if ((paramRSObject == null) || (!this.calc.pointOnScreen(localPoint))) {
        log("Not on screen " + paramString);
        return false;
      }

      this.mouse.move(localPoint);

      i = (int)this.calc.tileToScreen(paramRSObject.getLocation()).getX();
      j = (int)this.calc.tileToScreen(paramRSObject.getLocation()).getY();
      localPoint = new Point(i, j);
      if (!this.mouse.getLocation().equals(localPoint)) {
        return false;
      }
      String[] arrayOfString = this.menu.getItems();
      if (arrayOfString.length <= 1)
        return false;
      if (arrayOfString[0].toLowerCase().contains(paramString.toLowerCase())) {
        this.mouse.click(localPoint, true);
        return true;
      }
      this.mouse.click(localPoint, false);
      return this.menu.doAction(paramString);
    }
    catch (NullPointerException localNullPointerException) {
    }
    return true;
  }

  public int loop()
  {
    RSNPC localRSNPC = this.npcs.getNearest(new String[] { "Mr. Mordaut" });
    if (localRSNPC == null) {
      return -1;
    }

    if ((getMyPlayer().isMoving()) || (getMyPlayer().getAnimation() != -1)) {
      return random(800, 1200);
    }

    if (this.door != null) {
      if (this.calc.distanceTo(this.door) > 3) {
        this.walking.getPath(this.door.getLocation()).traverse();
        sleep(random(1400, 2500));
      }
      if (!this.calc.tileOnScreen(this.door.getLocation())) {
        this.walking.walkTileMM(this.door.getLocation());
        sleep(random(1400, 2500));
      }
      if (this.door.getID() == 2188) {
        this.camera.setCompass('w');
      }
      if (this.door.getID() == 2193) {
        this.camera.setCompass('e');
      }
      if (this.door.getID() == 2189) {
        this.camera.setCompass('w');
      }
      if (this.door.getID() == 2192) {
        this.camera.setCompass('n');
      }
      clickObject(this.door, "Open");
      return random(500, 1000);
    }
    RSComponent localRSComponent = searchInterfacesText("To exit,");
    if (localRSComponent != null) {
      if (localRSComponent.getText().toLowerCase().contains("red")) {
        this.door = this.objects.getNearest(new int[] { 2188 });
      }
      if (localRSComponent.getText().toLowerCase().contains("green")) {
        this.door = this.objects.getNearest(new int[] { 2193 });
      }
      if (localRSComponent.getText().toLowerCase().contains("blue")) {
        this.door = this.objects.getNearest(new int[] { 2189 });
      }
      if (localRSComponent.getText().toLowerCase().contains("purple")) {
        this.door = this.objects.getNearest(new int[] { 2192 });
      }
      return random(500, 1000);
    }
    if ((!this.interfaces.get(103).isValid()) && (!getMyPlayer().isMoving()) && (!this.interfaces.get(559).isValid()) && (!this.interfaces.canContinue()) && (this.door == null))
    {
      if (this.calc.distanceTo(localRSNPC) > 4) {
        this.walking.getPath(localRSNPC.getLocation()).traverse();
      }
      if (!this.calc.tileOnScreen(localRSNPC.getLocation())) {
        this.walking.walkTileMM(localRSNPC.getLocation());
      }
      clickCharacter(localRSNPC, "Talk-to");
      return random(1500, 1700);
    }
    if (this.interfaces.get(103).isValid()) {
      this.log.info("Question Type: Next Object");
      NextObjectQuestion localNextObjectQuestion = new NextObjectQuestion();
      if (localNextObjectQuestion.getObjects()) {
        if (localNextObjectQuestion.clickAnswer()) {
          return random(800, 1200);
        }
        localNextObjectQuestion.guess();
        return random(800, 1200);
      }

      this.log.info("Could not find get object. Making educated guess.");
      localNextObjectQuestion.guess();
      return random(800, 1200);
    }

    if (this.interfaces.get(559).isValid()) {
      this.log.info("Question Type: Similar Objects");
      int i = 0;
      for (SimilarObjectQuestion localSimilarObjectQuestion : this.simObjects) {
        if (localSimilarObjectQuestion.activateCondition()) {
          i = 1;
          if (localSimilarObjectQuestion.clickObjects()) {
            localSimilarObjectQuestion.accept();
          }
        }
      }
      if (i == 0) {
        this.log.severe("This is a new question.");
        this.log.severe("Please post this on the forums.");
        this.log.severe("The Missing Question is :");
        log(this.interfaces.get(103).getComponent(25).getText().toLowerCase());
      }
      return random(800, 1200);
    }

    if (this.interfaces.clickContinue()) {
      return random(800, 3500);
    }

    return random(800, 1200);
  }

  public RSComponent searchInterfacesText(String paramString) {
    RSInterface[] arrayOfRSInterface1 = this.interfaces.getAll();
    for (RSInterface localRSInterface : arrayOfRSInterface1) {
      for (RSComponent localRSComponent : localRSInterface.getComponents()) {
        if (localRSComponent.getText().toLowerCase().contains(paramString.toLowerCase()))
        {
          return localRSComponent;
        }
      }
    }
    return null;
  }

  public class SimilarObjectQuestion
  {
    String question;
    int[] Answers;

    public SimilarObjectQuestion(String paramArrayOfInt, int[] arg3)
    {
      this.question = paramArrayOfInt.toLowerCase();
      Object localObject;
      this.Answers = localObject;
    }

    public boolean accept() {
      return Exam.this.interfaces.getComponent(559, 26).doClick();
    }

    public boolean activateCondition() {
      if (!Exam.this.interfaces.get(559).isValid()) {
        return false;
      }
      if (Exam.this.interfaces.getComponent(559, 25).getText().toLowerCase().contains(this.question))
      {
        Exam.this.log.info("Question keyword: " + this.question);
        return true;
      }

      return false;
    }

    public boolean clickObjects() {
      int i = 0;
      for (int j = 42; j <= 56; j++) {
        for (int n : this.Answers) {
          if (Exam.this.interfaces.getComponent(559, j).getComponentID() == n) {
            if (Exam.this.interfaces.getComponent(559, j).doClick()) {
              Exam.this.sleep(Exam.this.random(600, 1000));
            }
            i++;
            if (i >= 3)
              return true;
          }
        }
      }
      Exam.this.log.info("returns false");
      return false;
    }
  }

  public class NextObjectQuestion
  {
    int One = -1;
    int Two = -1;
    int Three = -1;

    public NextObjectQuestion() {
    }

    public boolean arrayContains(int[] paramArrayOfInt, int paramInt) {
      int i = 0;
      for (int m : paramArrayOfInt) {
        if (m == paramInt) {
          i = 1;
        }
      }

      return i;
    }

    public boolean clickAnswer()
    {
      int[] arrayOfInt;
      if ((arrayOfInt = returnAnswer()) == null) {
        return false;
      }
      for (int i = 10; i <= 13; i++) {
        if (arrayContains(arrayOfInt, Exam.this.interfaces.get(103).getComponent(i).getComponentID()))
        {
          return Exam.this.interfaces.get(103).getComponent(i).doClick();
        }
      }
      return false;
    }

    public boolean getObjects() {
      this.One = -1;
      this.Two = -1;
      this.Three = -1;
      this.One = Exam.this.interfaces.get(103).getComponent(6).getComponentID();
      this.Two = Exam.this.interfaces.get(103).getComponent(7).getComponentID();
      this.Three = Exam.this.interfaces.get(103).getComponent(8).getComponentID();

      return (this.One != -1) && (this.Two != -1) && (this.Three != -1);
    }

    public void guess() {
      int[] arrayOfInt = new int[4];
      arrayOfInt[0] = Exam.access$500(Exam.this).get(103).getComponent(10).getComponentID();
      arrayOfInt[1] = Exam.access$600(Exam.this).get(103).getComponent(11).getComponentID();
      arrayOfInt[2] = Exam.access$700(Exam.this).get(103).getComponent(12).getComponentID();
      arrayOfInt[3] = Exam.access$800(Exam.this).get(103).getComponent(13).getComponentID();

      int i = 120;
      int j = 10;
      int k = 0;
      if (k <= 10) {
        Exam.this.interfaces.get(103).getComponent(Exam.this.random(10, 13)).doClick();
        return;
      }

      for (int m = 0; m < arrayOfInt.length; m++) {
        if (Math.abs(arrayOfInt[m] - k) <= i) {
          i = Math.abs(arrayOfInt[m] - k);
        }
        j = 10 + m;
      }
      Exam.this.interfaces.get(103).getComponent(j).doClick();
    }

    public int[] returnAnswer() {
      int[] arrayOfInt1 = new int[Exam.items.length];
      int i = 0;
      int j = 0;
      int k = 0;

      for (int m = 0; m < arrayOfInt1.length; m++) {
        arrayOfInt1[m] = 0;
      }

      for (int[] arrayOfInt2 : Exam.items) {
        for (int i5 : arrayOfInt2) {
          if (i5 == this.One) {
            i = 1;
          }
          if (i5 == this.Two) {
            j = 1;
          }
          if (i5 == this.Three) {
            k = 1;
          }
        }
      }
      if (i == 0) {
        Exam.this.log.severe("The first object is a new Object");
        Exam.this.log.severe("The Missing Object ID is :" + Integer.toString(this.One));
      }
      if (j == 0) {
        Exam.this.log.severe("The second object is a new Object");
        Exam.this.log.severe("The Missing Object ID is :" + Integer.toString(this.Two));
      }

      if (k == 0) {
        Exam.this.log.severe("The third object is a new Object");
        Exam.this.log.severe("The Missing Object ID is :" + Integer.toString(this.Three));
      }

      for (int n = 0; n < Exam.items.length; n++) {
        for (??? = 0; ??? < Exam.items[n].length; ???++) {
          if (Exam.items[n][???] == this.One) {
            arrayOfInt1[n] += 1;
          }
          if (Exam.items[n][???] == this.Two) {
            arrayOfInt1[n] += 1;
          }
          if (Exam.items[n][???] == this.Three) {
            arrayOfInt1[n] += 1;
          }
          if (arrayOfInt1[n] >= 2) {
            Exam.this.log.info("Answer Type Found!");
            return Exam.items[n];
          }
        }
      }

      return null;
    }
  }
}